package com.csu.ourpetstore1.controller;

import com.csu.ourpetstore1.domain.Account;
import com.csu.ourpetstore1.domain.Item;
import com.csu.ourpetstore1.domain.Order;
import com.csu.ourpetstore1.domain.Product;
import com.csu.ourpetstore1.service.AccountService;
import com.csu.ourpetstore1.service.CatalogService;
import com.csu.ourpetstore1.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AccountService accountService;

    @Autowired
    CatalogService catalogService;

    @Autowired
    OrderService orderService;

    @GetMapping("/SeeAllAccount")
    public String SeeAllAccount(Model model){
        List<Account> allAccount  = accountService.getAllAccount();
        model.addAttribute("allAccount",allAccount);
        return "Admin/allUsers";
    }

    @GetMapping("/GoToAGCatalog")
    public String GoToAGCatalog(Model model){
        List<Product> birds =catalogService.getProductListByCategory("BIRDS");
        List<Product> cats = catalogService.getProductListByCategory("CATS");
        List<Product> dogs = catalogService.getProductListByCategory("DOGS");
        List<Product> fish = catalogService.getProductListByCategory("FISH");
        List<Product> reptiles = catalogService.getProductListByCategory("REPTILES");
        model.addAttribute("birds",birds);
        model.addAttribute("dogs",dogs);
        model.addAttribute("cats",cats);
        model.addAttribute("fish",fish);
        model.addAttribute("reptiles",reptiles);
        return "Admin/product";
    }

    @GetMapping("/GoToAGAccount")
    public String GoToAGAccount(String username,Model model){
        Account account = accountService.getAccount(username);
        model.addAttribute("account",account);
        return "Admin/accountArrangement";
    }

    @GetMapping("/GoToItem")
    public String GoToItem(String productId,Model model){
        List<Item> items = catalogService.getItemListByProduct(productId);
        model.addAttribute("productId",productId);
        model.addAttribute("items",items);
        return "Admin/item";
    }

    @GetMapping("/main")
    public String main(){
        return "Admin/allUsers";
    }

    @GetMapping("/signin")
    public String GoToSignIn(){
        return "Admin/signIn";
    }

    @PostMapping("")
    public String checkSignIn(String username,String password){
        return "Admin/choose";
    }

    @GetMapping("/deleteProduct")
    public String deleteProduct(String productId){
        catalogService.deleteProduct(productId);
        return "Admin/choose";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(String itemId){
        catalogService.deleteItem(itemId);
        return "Admin/choose";
    }

    @GetMapping("/GoToAddProduct")
    public String GoToAddProduct(String categoryId,Model model){
        model.addAttribute("categoryId",categoryId);
        return "Admin/addProduct";
    }

    @GetMapping("/GoToAddItem")
    public String GoToAddItem(String productId,Model model){
        model.addAttribute("productId",productId);
        return "Admin/addItem";
    }

    @GetMapping("/AddProduct")
    public String AddProduct(@ModelAttribute Product product){
        catalogService.insertProduct(product);
        return "Admin/choose";
    }

    @PostMapping("/AddItem")
    public String AddItem(@ModelAttribute Item item){
        catalogService.insertItem(item);
        return "Admin/choose";
    }

    @GetMapping("/GoToAGOrder")
    public String ViewAllOrders(Model model){
        List<Order> orderList=orderService.getOrders();
        model.addAttribute("orderList",orderList);
        return "Admin/allOrders";
    }
}
